from flask import Flask,render_template,request,redirect
#创建一个对象实例
app = Flask(__name__)
#首页：
@app.route("/")
def index():
    return render_template('web.html')

#处理用户登录的请求
@app.route("/login",methods=['POST','GET'])
def login():
    username = request.form['username']#接受到输入的用户名
    userpwd = request.form['userpwd']#接受到输入的密码
    if username == '111':
        if userpwd == '222':
            return redirect("scrawl")#跳转到爬虫页面
        else:
            return redirect("/")#返回登录页面
    else:
        return redirect("/")#返回登录页面
    return "ok"

#爬虫作业：网页端表单设计
@app.route("/scrawl")
def scrawler():
    return render_template('scrawl.html')

@app.route("/start",methods=['POST','GET'])
def start():
    name = request.form['targetname']
    url = request.form['targeturl']
    #调用爬虫函数开始爬取数据
    return str(crawl(url,selector="p"))


import requests
from bs4 import BeautifulSoup
def crawl(url,selector):
    headers={
        "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36"
    }
    #获取源代码
    htmlcodes = requests.get(url,headers=headers).text
    #解析源代码
    bs = BeautifulSoup(htmlcodes,'html.parser')
    #根据选择器返回文本
    return [item.text for item in bs.select(selector=selector)]


#运行app实例
if __name__=="__main__":
    #导入权限
    app.run()